# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

cmake_minimum_required(VERSION 2.8.12)
project(bevfusion)

set(CMAKE_BUILD_PYTHON $ENV{USE_Python})
set(CMAKE_BUILD_TYPE "Release")
# set(CMAKE_BUILD_TYPE "Debug")
set(arch ${CMAKE_HOST_SYSTEM_PROCESSOR})

find_package(CUDA REQUIRED)

# set(Protobuf_Dir /usr/bin/protoc)
set(Protobuf_INCLUDE_DIRS "/usr/include")
set(Protobuf_LIBRARIES "/usr/lib/x86_64-linux-gnu/libprotobuf.so;-lpthread")
# find_package(Protobuf REQUIRED)

find_package(cv_bridge REQUIRED)
include_directories(${cv_bridge_INCLUDE_DIRS}) 

find_package(message_filters REQUIRED)
include_directories(${message_filters_INCLUDE_DIRS})

find_package(OpenCV 4 REQUIRED)
find_package(yaml-cpp REQUIRED)

set(spconv_include ./include)
set(spconv_lib ./lib/spconv/x86_64_cuda11.4)
message(STATUS "spconv_lib = ${spconv_lib}")


set(CMAKE_CXX_FLAGS_RELEASE "-std=c++14 -Wextra -Wall -Wno-missing-field-initializers -Wno-deprecated-declarations -O3 -DENABLE_TEXT_BACKEND_STB -Wno-unused-parameter")
set(CMAKE_CXX_FLAGS_DEBUG   "-std=c++14 -O0 -g -DENABLE_TEXT_BACKEND_STB")

# if("$ENV{SPCONV_CUDA_VERSION}" EQUAL "11.4")
#   # For cuda 11.x
#   set(CMAKE_CXX_FLAGS_RELEASE "-std=c++14 -Wextra -Wall -Wno-missing-field-initializers -Wno-deprecated-declarations -O3 -DENABLE_TEXT_BACKEND_STB -Wno-unused-parameter")
#   set(CMAKE_CXX_FLAGS_DEBUG   "-std=c++14 -O0 -g -DENABLE_TEXT_BACKEND_STB")
# else()
#   # For cuda 12.x
#   set(CMAKE_CXX_FLAGS_RELEASE "-std=c++17 -Wextra -Wall -Wno-missing-field-initializers -Wno-deprecated-declarations -O3 -DENABLE_TEXT_BACKEND_STB -Wno-unused-parameter")
#   set(CMAKE_CXX_FLAGS_DEBUG   "-std=c++17 -O0 -g -DENABLE_TEXT_BACKEND_STB")
# endif()

set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} 
  -gencode arch=compute_89,code=compute_89
)

set(tensorrt_dir /home/xd/TensorRT-8.5.1.7.Linux.x86_64-gnu.cuda-11.8.cudnn8.6/TensorRT-8.5.1.7)
set(tensorrt_lib ${tensorrt_dir}/lib)
set(tensorrt_target_lib ${tensorrt_dir}/targets/x86_64-linux-gnu/lib)
set(tensorrt_include ${tensorrt_dir}/include)

include_directories(
  ${Protobuf_INCLUDE_DIRS}
  ${CUDA_INCLUDE_DIRS}
  ${tensorrt_include}
  # $ENV{Python_Inc}
  ./include/cuosd/src
  ${spconv_include}
  src/common
  src/
)
message(STATUS "Protobuf_INCLUDE_DIRS = ${Protobuf_INCLUDE_DIRS}")

set (cuda_lib /usr/local/cuda/lib64)


link_directories(
  ${Protobuf_LIBRARIES}
  ${cuda_lib}
  ${tensorrt_lib}
  ${spconv_lib}
  ${tensorrt_target_lib}
  build
  # $ENV{Python_Lib}
)
message(STATUS "tensorrt_lib = ${tensorrt_lib}")
message(STATUS "cuda_lib = ${cuda_lib}")
message(STATUS "tensorrt_lib = ${tensorrt_lib}")
message(STATUS "spconv_lib = ${spconv_lib}")
message(STATUS "tensorrt_target_lib = ${tensorrt_target_lib}")

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

file(GLOB_RECURSE CORE_FILES 
  src/bevfusion/*.cu 
  src/bevfusion/*.cpp
  src/onnx/*.cpp
  src/common/tensor.cu
  src/common/tensorrt.cpp
  src/bevfusiondetect.cpp
  src/bevfusiondetect.h
)

file(GLOB_RECURSE VISUALIZE_FILES 
  ./include/cuosd/src/*.cpp
  ./include/cuosd/src/*.cu
  src/common/visualize.cu
)

####################### libbevfusion_core.so ##########################
cuda_add_library(bevfusion_core SHARED 
  ${CORE_FILES}
)

target_link_libraries(bevfusion_core
  libcudart.so
  libcublasLt.so
  libnvinfer.so
  libspconv.so
  ${Protobuf_LIBRARIES}
  libnvinfer_plugin.so
  ${cv_bridge_LIBRARIES} 
  ${message_filters_LIBRARIES}
)
#######################################################################


option(ROS_ENABLE "option for ros enable" OFF)
if(ROS_ENABLE)
	add_definitions(-DROS_ENABLE)
	find_package(roscpp REQUIRED)
  find_package(visualization_msgs REQUIRED)
	include_directories(
	${roscpp_INCLUDE_DIRS}
	)
endif()

if(ROS_ENABLE)
	target_link_libraries(bevfusion_core ${roscpp_LIBRARIES})
endif()


####################### bevfusion program #############################
cuda_add_executable(bevfusion 
  src/main.cpp
  ${VISUALIZE_FILES}
)

target_link_libraries(bevfusion
  bevfusion_core
  libnvinfer.so
  libspconv.so
  ${Protobuf_LIBRARIES}
  libnvinfer_plugin.so
  ${OpenCV_LIBS}
  ${YAML_CPP_LIBRARIES}
)
#######################################################################


########################## custom_layernorm.so ################################
cuda_add_library(custom_layernorm SHARED
  src/plugins/custom_layernorm.cu
)

target_link_libraries(custom_layernorm
  libnvinfer.so
  libnvinfer_plugin.so
)
#######################################################################


########################## libpybev.so ################################
# if(${CMAKE_BUILD_PYTHON} STREQUAL "ON")
#   cuda_add_library(pybev SHARED
#     src/python.cpp
#   )
#   target_link_libraries(pybev
#     bevfusion_core
#     libnvinfer.so
#     libspconv.so
#     ${Protobuf_LIBRARIES}
#     libnvinfer_plugin.so
#     $ENV{Python_Soname}
#   )
# endif()
#######################################################################
